<?php

namespace app\api\controller\mall;

use addons\litestore\model\Litestoreorder;
use addons\litestore\model\Litestoreordergoods;
use app\common\controller\Api;
use app\common\model\meet\Config;

class Refund extends Api
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $page = $this->request->get('page/d', 1);
        $page_size = $this->request->get('page_size/d', 10);

        $condition = [
            'user_id' => $this->auth->id,
            'is_del' => 0
        ];

        $model = new \app\common\model\mall\Refund();

        $list = $model
            ->with(['goods' => ['spec', 'goods'], 'log'])
            ->where($condition)
            ->field('id,order_goods_id,refund_status,express_company,express_no,refund_type,refund_money')
            ->order('createtime', 'desc')
            ->paginate(['list_rows' => $page_size, 'page' => $page]);

        foreach ($list as $key => $value) {
            $value['goods']['image'] = cdnurl(explode(",", $value['goods']['images'])[0], true);
            $value['goods']['sku_image'] = $value['goods']['spec']['spec_image'] == $value['goods']['image'] ? '' : cdnurl($value['goods']['spec']['spec_image'], true);
        }

        $this->success('', $list);
    }

    public function detail()
    {
        $id = $this->request->get('id/d', 0);

        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $condition = [
            'user_id' => $this->auth->id,
            'id' => $id
        ];

        $model = new \app\common\model\mall\Refund();

        $detail = $model
            ->with(['goods', 'log'])
            ->where($condition)
            ->field('id,order_goods_id,refund_status,express_company,express_no,refund_type,refund_money,refund_no,refund_reason,refund_images,refund_explain,createtime,receive_name,receive_mobile,receive_address,refund_refuse_reason')
            ->find();

        if (empty($detail)) {
            $this->error(__('No results were found'));
        }

        if ($detail['goods']) {
            $detail->goods->image = cdnurl(explode(",", $detail->goods->images)[0], true);

            $detail->goods->sku_image = $detail->goods->spec->spec_image == '' ? $detail->goods->image : cdnurl($detail->goods->spec->spec_image, true);
        }


        $this->success('', $detail);
    }

    public function info()
    {
        $id = $this->request->get('id/d', 0);

        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $order_goods_info = Litestoreordergoods::with(['spec', 'goods'])->where(['id' => $id, 'user_id' => $this->auth->id])->find();

        if (empty($order_goods_info)) {
            $this->error(__('No results were found'));
        }

        $order_model = new Litestoreorder();
        $order_info = $order_model->where('id', '=', $order_goods_info['order_id'])->find();

        if (empty($order_info)) {
            $this->error(__('No results were found'));
        }

        if ($order_info['pay_status'] != 20) {
            $this->error(__('The order has not been paid and cannot be refunded'));
        }

        if ($order_info['order_status'] >= 30) {
            $this->error(__('The order has been completed and cannot be refunded'));
        }


        $order_goods_info['image'] = cdnurl(explode(",", $order_goods_info['images'])[0], true);
        $order_goods_info['sku_image'] = $order_goods_info['spec']['spec_image'] == '' ? $order_goods_info['image'] : cdnurl($order_goods_info['spec']['spec_image'], true);


        //退款原因
        $refund_reason_list = Config::getConfigByName('mall_refund_reason')['value'] ? json_decode(Config::getConfigByName('mall_refund_reason')['value'], true) : [];

        //退款类型
        if ($order_info['pay_status'] == 20 && $order_info['freight_status'] == 10 && $order_info['order_status'] == 10 && $order_info['receipt_status'] == 10) {
            $refund_type_list = [
                ['type' => 1, 'title' => '我要退款(无需退货)', 'desc' => '没有收到货,或与卖家协商同意不用退货只退款']];
        } else {
            $refund_type_list = [
                ['type' => 1, 'title' => '我要退款(无需退货)', 'desc' => '没有收到货,或与卖家协商同意不用退货只退款'],
                ['type' => 2, 'title' => '我要退货退款', 'desc' => '已收到货,需要退还收到的货']
            ];
        }

        $data = [
            'order_goods_info' => $order_goods_info,
            'refund_money' => $order_goods_info['total_price'],
            'refund_reason_list' => $refund_reason_list,
            'refund_type_list' => $refund_type_list
        ];

        $this->success('', $data);
    }

    public function submit()
    {
        $post = $this->request->post();

        if (empty($post)) {
            $this->error(__('Invalid parameters'));
        }

        $orderLogic = new \app\common\logic\mall\Order();

        $result = $orderLogic->refundOrderSubmimt($post, $this->auth->getUserinfo());

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }

    public function cancel()
    {
        $id = $this->request->post('id/d', 0);

        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $orderLogic = new \app\common\logic\mall\Order();

        $result = $orderLogic->refundCancel($id, $this->auth->getUserinfo());

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }

    public function del()
    {
        $id = $this->request->post('id/d', 0);

        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $orderLogic = new \app\common\logic\mall\Order();

        $result = $orderLogic->refundDel($id);

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }

    public function delivery(){
        $post = $this->request->post();

        if (empty($post)) {
            $this->error(__('Invalid parameters'));
        }

        $orderLogic = new \app\common\logic\mall\Order();

        $result = $orderLogic->refundDeliveryByUser($post, $this->auth->getUserinfo());

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }
}


